package com.landiao.service;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.landiao.common.weixin.WxConsts;
import com.landiao.common.weixin.WxErrorException;
import com.landiao.common.weixin.WxMpService;
import com.landiao.common.weixin.bean.WxMpMaterialUploadResult;
import com.landiao.entities.Material;
import com.landiao.mapper.MaterialMapper;
@Service
public class MaterialService extends BaseService<Material, String>{
	@Autowired
	private WxMpService vipWxService;
	@Override
	protected MaterialMapper getBaseMapper() {
		// TODO Auto-generated method stub
		return (MaterialMapper)baseMapper;
	}

	public void save(Material t,String url) {
		WxMpMaterialUploadResult result;
		try {
			result = vipWxService.materialFileUpload(WxConsts.MEDIA_IMAGE, new File(url));
			t.setMediaId(result.getMedia_id());
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		getBaseMapper().insert(t);
	}

	public List<Material> getAllMaterialList(Material material) {
		// TODO Auto-generated method stub
		return getBaseMapper().getAllMaterialList(material);
	}
	/**
	 * 判断生日关怀类型是否存在
	 * @param material
	 * @return
	 */
	public Material getByPurpose(Material material) {
		// TODO Auto-generated method stub
		return getBaseMapper().getByPurpose(material);
	}

	public void update(Material t, String url) {
		if(url !=null){
			WxMpMaterialUploadResult result;
			try {
				result = vipWxService.materialFileUpload(WxConsts.MEDIA_IMAGE, new File(url));
				t.setMediaId(result.getMedia_id());
			} catch (WxErrorException e) {
				e.printStackTrace();
			}
		}
		getBaseMapper().updateByPrimaryKeySelective(t);
	}

}
